<?php


namespace App\Http\Controllers\Forwarder;


use App\Http\Controllers\Controller;
use App\Models\BadgeUser;
use Illuminate\Http\Request;

class BadgeUserController extends Controller
{
    public function index(Request $request)
    {
        $list = BadgeUser::with(['badge'])->where('forwarder_id', $request->user()->forwarder_id)->orderBy('sort', 'desc')->get();
        return $this->response($list);
    }

    public function update($id, Request $request)
    {
        if ($request->input('sort') > 0){
            BadgeUser::where('user_id', $request->user()->id)->where('sort', $request->input('sort'))->update(['sort' => 0]);
        }
        $obj = BadgeUser::where('user_id', $request->user()->id)->where('id', $id)->first();
        $obj->update($request->all());
        return $this->response();
    }

}
